package com.toe.business.webapi.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.toe.common.restful.JsonResult;
import com.toe.common.restful.StateEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private static final ObjectMapper objectMapper=new ObjectMapper();
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //if里面填写拦截逻辑
        if(1==2) {
            //在这里返回一个包含着错误信息的对象数据
            JsonResult jsonResult = JsonResult.failed(StateEnum.ERR_UNAUTHORIZED, "您没有登录！");
            httpServletResponse.setContentType("application/json;charset=utf-8");
            httpServletResponse.getWriter().write(objectMapper.writeValueAsString(jsonResult));
            httpServletResponse.flushBuffer();
        }
    }
}
